
export function getNow(){
    return new Date().getTime();
}

export function clearTimer() {
    let btn = document.getElementById("marker");

    if (typeof btn === "object" && btn) {
        let btnRect = btn.getBoundingClientRect();
        btn.style.left = (0 - btnRect.width / 2 - 1) + "px";
    }
    if (!chrome.action) {
        return;
    }
    chrome.action.setBadgeText({text: ""});
}

export function startCountHint() {
    if(chrome.alarms){
        chrome.alarms.clearAll();
        chrome.alarms.create("onCountdown", {when: Date.now() + 200});
        chrome.alarms.onAlarm.addListener(updateCounter);
    }
}

function updateCounter(state){
    let str = defineTimeToLeftBadgeString(state);

    chrome.action.setBadgeText({text: str});
    if(timeToDisplay && timeToDisplay !== ''){
        // chrome.alarms.create("onCountdown", {when: Date.now() + 200});
        // chrome.alarms.onAlarm.addListener(updateCounter);
    }
}

function millisecondsToDays(val) {
    if(val<0){val = 0;}
    const millisecondsPerDay = 24 * 60 * 60 * 1000;
    return Math.floor(val / millisecondsPerDay);
}

function millisecondsToHours(val) {
    if(val<0){val = 0;}
    const millisecondsPerHour = 60 * 60 * 1000;
    return Math.floor(val / millisecondsPerHour);
}

function millisecondsToMinutes(val) {
    if(val<0){val = 0;}
    const millisecondsPerMinute = 60 * 1000;
    return Math.floor(val / millisecondsPerMinute);
}

function millisecondsToSeconds(val) {
    if(val<0){val = 0;}
    const millisecondsPerSec = 1000;
    return Math.floor(val / millisecondsPerSec);
}

function daysToMilliseconds(val) {
    if(val<0){val = 0;}
    const millisecondsPerDay = 24 * 60 * 60 * 1000;
    return val * millisecondsPerDay;
}

function hoursToMilliseconds(val) {
    if(val<0){val = 0;}
    const millisecondsPerHour = 60 * 60 * 1000;
    return val * millisecondsPerHour;
}

function minutesToMilliseconds(val) {
    if(val<0){val = 0;}
    const millisecondsPerMinute = 60 * 1000;
    return val * millisecondsPerMinute;
}

function secondsToMilliseconds(val) {
    if(val<0){val = 0;}
    const millisecondsPerSec = 1000;
    return val * millisecondsPerSec;
}

export function defineTimeToLeftObject(ms){

    let nowMs = new Date().getTime();
    let targetMs = new Date(ms).getTime();
    let diff = targetMs - nowMs;

    let days = millisecondsToDays(diff);
    diff -= daysToMilliseconds(days);

    let hours = millisecondsToHours(diff);
    diff -= hoursToMilliseconds(hours);

    let minutes = millisecondsToMinutes(diff);
    diff -= minutesToMilliseconds(minutes);

    let seconds = millisecondsToSeconds(diff);
    diff -= secondsToMilliseconds(seconds);

    return {
        d: days,
        h: hours,
        m: minutes,
        s: seconds
    };
}

export function defineTimeToLeftBadgeString(state) {
    let obj = defineTimeToLeftObject(state.countTo);
    let d = obj.d;
    let h = obj.h;
    let m = obj.m;
    let s = obj.s;

    if(m > 0){
        return m+"m";
    } else {
        return s+"s";
    }

    // if(d>0) {
    //     d += 1;
    //     return d + "d" + getIndicator(s);
    // }
    // if(h>0) {
    //     h += 1;
    //     return h + "h" + getIndicator(s);
    // }
    //
    // if (m > 0) {
    //     m += 1;
    //     return m + "m" + getIndicator(s);
    // }
    // return s + "s";
}

function getIndicator(s){
    let indicator = ".";
    if(s && s % 2 === 0){
        indicator = ":";
    }
    return indicator;
}


export function onRankClick(target){
    let num = target.target.getAttribute("data-rank")*1;

    let href = "https://forms.gle/xJXyohzifE8xnGFP7";
    if(num>3){
        const extensionId = chrome.runtime.id;
        href = `https://chromewebstore.google.com/detail/${extensionId}/reviews`
    }
    chrome.tabs.create({ url: href });
}

export function highlightAllRankTill(target){
    let num = target.target.getAttribute("data-rank")*1;
    document.querySelectorAll("[data-rank]").forEach(element => {
        let ind = element.getAttribute("data-rank")*1;
        element.classList.remove("on");
        if(ind < num){
            element.classList.add("on");
        }
    })
}

export function clearRankHighlight(){
    document.querySelectorAll("[data-rank]").forEach(element => {
        element.classList.remove("on");
    })
}

